<template>
  <el-dialog v-model="visible" :close-on-click-modal="false" :close-on-press-escape="false">
    <div style="display: flex; justify-content: space-between;margin-bottom: 20px;padding-top: 10px">
      <div style="display: flex; align-items: center">
        <div class="p-flex">
          <span style="width: 60px">选择通道</span>
        </div>
      </div>
      <div>
        <el-button type="primary" @click="screenshot()">抓图</el-button>
      </div>
    </div>

    <el-table :data="crmListall" row-key="id" @select="selectChange" ref="multipleTable" @select-all="selectAllChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column property="name" label="通道"/>
    </el-table>
  </el-dialog>
</template>

<script>
import baseService from "@/service/baseService";
import {ElMessage} from "element-plus";

const tableList = [{
  name: "通道一",
  id: 1
}, {
  name: "通道二",
  id: 2
}, {
  name: "通道三",
  id: 3
}, {
  name: "通道四",
  id: 4
}, {
  name: "通道五",
  id: 5
}, {
  name: "通道六",
  id: 6
}, {
  name: "通道七",
  id: 7
}, {
  name: "通道八",
  id: 8
}, {
  name: "通道九",
  id: 9
}, {
  name: "通道十",
  id: 10
}]

export default {
  data() {
    return {
      tableData: [], //初始数据
      crmListall: [], //筛选展示数据
      checkAll: false,
      checked: [],
      tableAddr: "",
      secondNum: 5,
      checkedlook: false,
      cirblooun: false,
      setIntervalTd: null,
      visible: false,
      stationId: ""
    }
  },
  watch: {
    checkedlook() {
      if (this.checkedlook) {
        this.crmListall = this.checked;
      } else {
        this.crmListall = this.tableData;
      }
    }
  },
  methods: {
    init(stationId) {
      this.visible = true;
      this.$nextTick(() => {
        this.stationId = stationId;
        this.crmListall = tableList;
        // this.getMapList();
        console.log(this.checked)
        this.toggleSelection(this.checked);
      });
    },
    // 数据回显
    toggleSelection(rows) {
      this.$nextTick(() => {
        setTimeout(() => {
          if (rows && this.crmListall.length != 0) {
            this.crmListall.forEach((row) => {
              rows.forEach((item) => {
                if (row.id == item.id) {
                  console.log(item.name)
                  this.$refs.multipleTable.toggleRowSelection(row, true)
                }
              })
            })
          } else {
            this.$refs.multipleTable.clearSelection();
          }
        }, 500);
      });
    },
    selectChange(selection) {
      this.checked = selection;
    },
    selectAllChange(selection) {
      this.checked = selection;
    },
    screenshot() {
      const ids = this.checked.map(function (item) {
        return item.id
      });
      let pamers = {
        stationId: this.stationId,
        channelList: ids
      }
      console.log(ids)
      baseService.post(`/KxWarningPic/kxwarningpic/sendCmdTakePhone`, [pamers]).then((res) => {
        if (res.code !== 0) {
          return ElMessage.error(res.msg);
        }
        ElMessage.success("命令下发成功，请稍后刷新列表！");
        this.visible = false;
        this.$emit("close");
      });
    }
  }
}
</script>

<style scoped lang="scss">
.p-flex {
  display: flex;
  align-items: center;
  padding-right: 20px;
}
</style>
